class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for(int i=0;i<s.length();i++){
            char ch=s.charAt(i);
            if(ch=='{'||ch=='['||ch=='('){  //遇到左括号就进栈
                stack.push(ch);
            }else{
                //遇到右括号
                //如果栈为空，返回FALSE
                if(stack.empty()){
                    return false;
                } 
                char ch1=stack.peek();//左括号的栈顶元素
             if(ch==')'&&ch1=='('||ch=='}'&&ch1=='{'||ch==']'&&ch1=='['){
                stack.pop();
            }else{
                        return false;
                    }
                
            }
        }
        if(!stack.empty()){
            return false;
        }

            return true;
    }
}